package cn.xml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @auth wzy
 * @date 2023/9/27 16:22
 */
public class Test4 {

    public static void main(String[] args) throws DocumentException, IOException {
        SAXReader reader = new SAXReader();

        Document doc = reader.read("resources/phone.xml");

        Element root = doc.getRootElement();

        Element elementBrand = root.addElement("brand");
        elementBrand.addAttribute("name","三星");

        Element elementType = elementBrand.addElement("type");
        elementType.addAttribute("name","S22");

        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("GBK");

        XMLWriter writer = new XMLWriter(new FileWriter("resources/phone2.xml"),format);

        writer.write(doc);

        writer.close();
    }
}
